﻿using SheepCultivation.Common.EFDb;
using SheepCultivation.Common.Infrastructure.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SheepCultivation.Common.Infrastructure.Repository
{
    public class BaseRepsoitory<T> : IBaseRepository<T> where T : class, new()
    {
        public readonly EFDBContext db;

        public BaseRepsoitory(EFDBContext db)
        {
            this.db = db;
        }

        public int Add(T entity)
        {
            db.Set<T>().Add(entity);
            return db.SaveChanges();
        }
        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int AddRange(List<T> entities)
        {
            db.Set<T>().AddRange(entities);
            return db.SaveChanges();
        }

        public IQueryable<T> GetAll()
        {
            return db.Set<T>().AsQueryable();
        }
        public int Update(T entity)
        {
            db.Set<T>().Update(entity);
            return db.SaveChanges();
        }
        //public async Task< int> AddAsync(T entity)
        //{
        //    db.Set<T>().AddAsync(entity);
        //    return await db.SaveChangesAsync();
        //}
    }
}
